package com.fanshuai.springcloud.controller;

import com.fanshuai.springcloud.domain.Book;
import com.fanshuai.springcloud.domain.BookDTO;
import com.fanshuai.springcloud.domain.User;
import com.fanshuai.springcloud.outer.bookservice.BookService;
import com.fanshuai.springcloud.outer.userservice.UserService;
import com.fanshuai.springcloud.request.AddRequest;
import com.fanshuai.springcloud.web.enums.StatusCode;
import com.fanshuai.springcloud.web.exception.FanshuaiException;
import com.fanshuai.springcloud.web.response.CallResult;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/web")
public class WebController {
    @Resource
    private BookService bookService;
    @Resource
    private UserService userService;

    @GetMapping("/list")
    public CallResult<List<BookDTO>> list() {
        CallResult<List<Book>> result = bookService.list();
        if (!result.isSuccess() || CollectionUtils.isEmpty(result.getData())) {
            return new CallResult<>(new ArrayList<>());
        }

        List<BookDTO> list = result.getData().stream().map(book -> {
            BookDTO bookDTO = new BookDTO();
            bookDTO.setBook(book);
            return bookDTO;
        }).collect(Collectors.toList());

        list.forEach(book -> {
            CallResult<User> userCallResult = userService.one(book.getBook().getAuthor());
            if (userCallResult.isSuccess()) {
                book.setUser(userCallResult.getData());
            }
        });

        return new CallResult<>(list);
    }

    @PostMapping(value = "/web/add", consumes = MediaType.APPLICATION_JSON_VALUE)
    public CallResult<Boolean> add(@RequestParam Map<String, Object> params,
                                   @RequestBody AddRequest request) {

        return addInternal(params, request);
    }

    @PostMapping(value = "/web/add1", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public CallResult<Boolean> add1(@RequestParam Map<String, Object> params,
                                    @RequestBody AddRequest request) {

        return addInternal(params, request);
    }

    private CallResult<Boolean> addInternal(Map<String, Object> params, AddRequest request) {
        long userId = MapUtils.getLongValue(params, "userId");
        long bookId = MapUtils.getLongValue(params, "bookId");
        if (userId <= 0 || bookId <= 0) {
            throw new FanshuaiException(StatusCode.FAILED);
        }

        User user = new User();
        user.setId(userId);
        user.setName(request.getUserName());
        userService.add(user);

        Book book = new Book();
        book.setId(bookId);
        book.setBookName(request.getBookName());
        book.setDesc(request.getBookDesc());
        book.setAuthor(userId);

        bookService.add(book);

        return new CallResult<>(true);
    }
}
